Skip to content

Conversation

@ibc
Copy link
Member

@ibc ibc commented Jan 22, 2025

I cannot explain it better than I've done in the code and tests.

NOTES

We cannot do much magic here, and we should not make happy assumptions. For example, in Node.js when you create a Node native Buffer instance using Buffer.allocUnsafe(), its buffer member (an ArrayBuffer instance where the bytes really are) is a **very big allocated ArrayBuffer instance. If you later create another Node native Buffer then its .buffer member will be the same as in the previous one, however the byteOffset and so off are different:

> b1 = Buffer.allocUnsafe(10)
// <Buffer 00 00 00 00 00 00 00 00 00 00>

> b1.buffer
// ArrayBuffer {
//   [Uint8Contents]: <2f 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
//    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
//    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
//    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
//    ...8092 more bytes>,
//   byteLength: 8192
// }

> b2 = Buffer.allocUnsafe(10)
// <Buffer 00 00 00 00 00 00 00 00 00 00>

> b2.buffer
// ArrayBuffer {
//   [Uint8Contents]: <2f 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
//    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
//    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
//    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
//    ... 8092 more bytes>,
//   byteLength: 8192
// }

> b1.buffer === b2.buffer
// true

> b1.byteOffset === b2.byteOffset
// false

I cannot explain it better than I've done in the code and tests.
@ibc ibc merged commit 8ffb854 into master Jan 23, 2025
7 checks passed
@ibc ibc deleted the make-serialize-throw-if-same-buffer-and-byteoffset-are-given branch January 23, 2025 09:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants